package cn.zzf.core;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author GaoFeng2017
 * @date 2018-02-20 11:00:44
 **/

public class LinkHandler implements ContentHandler{
    @Override
    public void handle(String content, String tid) {
        System.out.println(content.substring(content.indexOf("教师："),content.indexOf("教师：") + 8));
        String main = content.substring(content.indexOf("地点"),content.length());
        Pattern pattern = Pattern.compile("<tr>(?s)((?!tr).)+(4%)((?!tr).)+(24%)((?!tr).)+</tr>",Pattern.DOTALL);
        Matcher matcher = pattern.matcher(main);
        String teacher = content.substring(content.indexOf("教师："),content.indexOf("教师：") + 8).replaceAll("教师：([^\\&nbsp;]+).+","$1");
        while (matcher.find()) {
            System.out.println("yes");
            String str = teacher + "!!";
            String column = matcher.group();
            Pattern cNamePattern = Pattern.compile("<td style='width:24%;text-align:left'(\\shidevalue[^<>]+)?>[^<>]*</td>");

            Matcher cNameMatcher = cNamePattern.matcher(column);
            if(cNameMatcher.find()) {
                str += cNameMatcher.group() + "!!";
            }

            Pattern cTypePattern = Pattern.compile("<td style='width:8%;text-align:center'(\\shidevalue[^<>]+)?>[^<>]*</td>");
            Matcher cTypeMatcher = cTypePattern.matcher(column);

            if (cTypeMatcher.find()) {
                str += cTypeMatcher.group() + "!!";
            }

            Pattern cMorePattern = Pattern.compile("<td style='width:5%;text-align:right'(\\shidevalue[^<>]+)?>[^<>]*</td>");
            Matcher cMoreMatcher = cMorePattern.matcher(column);

            while (cMoreMatcher.find()) {
                str += cMoreMatcher.group() + "!!";
            }

            Pattern cWeeksPattern = Pattern.compile("<td style='width:9%;text-align:center'(\\shidevalue[^<>]+)?>[^<>]*</td>");
            Matcher cWeeksMatcher = cWeeksPattern.matcher(column);

            if (cWeeksMatcher.find()) {
                str += cWeeksMatcher.group() + "!!";
            }

            Pattern cClassPattern = Pattern.compile("<td style='width:12%;text-align:left'(\\shidevalue[^<>]+)?>[^<>]*</td>");
            Matcher cClassMatcher = cClassPattern.matcher(column);

            if (cClassMatcher.find()) {
                str += cClassMatcher.group() + "!!";
            }

            str = str.replaceAll("<","")
                    .replaceAll(">","")
                    .replaceAll("td","")
                    .replaceAll("(style='[^\\s]+')","")
                    .replaceAll("hidevalue='([^\\s]+)'","$1")
                    .replaceAll("\\s","")
                    .replaceAll("/","");

            Map<String,String> map = parseContent(str);
            try {
                CrawerUtils.saveToProperties(map,"h:\\2018_link\\" + UUID.randomUUID().toString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public Map<String, String> parseContent(String content) {
        System.out.println(content);
        String[] items = content.split("!!");
        String[] names = {"teacher","name","category","credit","weeks","teams","total_people","week","class"};
        Map<String,String> map = new LinkedHashMap<>();
        for(int i = 0;i < names.length;i++) {
            map.put(names[i],items[i]);
        }
        return map;
    }

}
